package com.doctor.database.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.doctor.database.dao.BaseDao;
import com.doctor.database.model.Patient;
import com.doctor.database.repository.PatientRepository;
import com.doctor.utils.ResponseUtil;

@Service("patientService")
public class PatientService {
	
	private static final Logger LOGGER = Logger.getLogger(PatientService.class);
	
	@Autowired
	BaseDao dao;
	
	@Autowired
	PatientRepository patientRepository;
	
	
	@Transactional
	public Map create(String patient){
		Patient newPatient=JSON.parseObject(patient, Patient.class);
		Map map = new HashMap<String, Object>();		
		if(newPatient.getId()!=null ){
			dao.delete(newPatient.getId(), Patient.class);
		}
		
		Patient result	=dao.save(newPatient , Patient.class);
		result.getId();
		if(result!=null){
			map.put("returnMessage", "保存成功!");
			map.put("returnCode", 1);
			return map;
		}
		map.put("returnMessage", "保存失败!");
		map.put("returnCode", 0);
		return map;
	}
	
	@Transactional
	public Map queryPatients(Map parma){
		
		final String accountId = (String) parma.get("accountId");
		int page =Integer.parseInt(parma.get("page").toString());//第几页
		int pageSize=Integer.parseInt(parma.get("pageSize").toString());//一页几条
		
		Map resultMap = new HashMap<String, Object>();
		Pageable pager =new PageRequest(page, pageSize); 
		Specification<Patient> Specification = new Specification<Patient>() {
			@Override
			public Predicate toPredicate(Root<Patient> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				Path<Long> accountIds = root.get("accountId");    
				query.where(cb.equal(accountIds,accountId));
				return null;
			}
		};
		 Page<Patient> pages =dao.getPageByFilter(pager , Specification , Patient.class);
		 if(pages.getContent() !=null){
			 resultMap.put("returnMessage", "查询数据成功!");
			 resultMap.put("returnCode", 1);
			 resultMap.put("data", pages.getContent());	
			 resultMap.put("number", pages.getNumber());	
			 resultMap.put("size", pages.getTotalPages());
		 }else{
			 resultMap.put("returnMessage", "没有查询到数据!");
			 resultMap.put("returnCode", 1);
			 resultMap.put("data", new ArrayList<Patient>());	
		 }
		return resultMap;
	}
	

	@Transactional
	public  Map deletePatient(Long id){
		Map resultMap = new HashMap<String, Object>();
		try{
			dao.delete(id, Patient.class);	
			 resultMap.put("returnMessage", "删除成功!");
			 resultMap.put("returnCode", 1);
		}catch(Exception e){
			 resultMap.put("returnMessage", "删除失败!");
			 resultMap.put("returnCode", 0);
		}
		return resultMap;
	}
}
